package com.rain.builder;

import com.rain.bean.FieldInfo;
import com.rain.bean.TableInfo;
import com.rain.utils.Constants;
import com.rain.utils.DateUtils;
import com.rain.utils.StringUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.List;
import java.util.Map;

public class BuildMapper {

    private static final Logger logger = LoggerFactory.getLogger(BuildMapper.class);

    public static void execute(TableInfo tableInfo) throws IOException {
        File folder = new File(Constants.PACKAGE_MAPPER_PATH);
        if(!folder.exists()){
            folder.mkdirs();
        }

        String className = tableInfo.getBeanName()+"Mapper";
        File file = new File(folder, className + ".java");

        try{
            file.createNewFile();
        }catch(IOException e){
            e.printStackTrace();
        }

        OutputStream outputStream = null;
        OutputStreamWriter outputStreamWriter = null;
        BufferedWriter bufferedWriter = null;

        try{
            outputStream = new FileOutputStream(file);
            outputStreamWriter = new OutputStreamWriter(outputStream,"UTF-8");
            bufferedWriter = new BufferedWriter(outputStreamWriter);
            // 基本模版
            bufferedWriter.write("package " + Constants.PACKAGE_MAPPER + ";");
            bufferedWriter.newLine();
            bufferedWriter.newLine();
            bufferedWriter.write("import " + Constants.PACKAGE_MAPPER + ".BaseMapper" +";");
            bufferedWriter.newLine();
            bufferedWriter.write("import org.apache.ibatis.annotations.Param;");
            bufferedWriter.newLine();
            bufferedWriter.newLine();



            // 构建类注释
            BuildComment.createClassComment(bufferedWriter,tableInfo.getComment());
            bufferedWriter.write("public interface " + className + "<T,P> extends BaseMapper {");
            bufferedWriter.newLine();
            bufferedWriter.newLine();


            Map<String, List<FieldInfo>>  keyIndexMap = tableInfo.getKeyIndexMap();
            for (Map.Entry<String,List<FieldInfo>> entry: keyIndexMap.entrySet()) {
                List<FieldInfo> value = entry.getValue();
                Integer index = 0;
                StringBuffer stringBufferName = new StringBuffer();
                StringBuffer stringBufferParam = new StringBuffer();
                for (FieldInfo fieldInfo : value) {
                    index++;
                    stringBufferName.append(StringUtils.uperCaseFirstLetter(fieldInfo.getPropertyName()));
                    if(index < value.size()){
                        stringBufferName.append("And");
                    }

                    stringBufferParam.append("@Param(\"" + fieldInfo.getPropertyName() + "\") " + fieldInfo.getJavaType() + " " + fieldInfo.getPropertyName());
                    if(index < value.size()){
                        stringBufferParam.append(", ");
                    }
                }
                BuildComment.createFieldComment(bufferedWriter,"根据" + stringBufferName + "查询");
                bufferedWriter.write("\tT selectBy" + stringBufferName + "(" + stringBufferParam + ");");
                bufferedWriter.newLine();
                bufferedWriter.newLine();

                BuildComment.createFieldComment(bufferedWriter,"根据" + stringBufferName + "更新");
                bufferedWriter.write("\tInteger updateBy" + stringBufferName + "(@Param(\"bean\") T t, " + stringBufferParam + ");");
                bufferedWriter.newLine();
                bufferedWriter.newLine();

                BuildComment.createFieldComment(bufferedWriter,"根据" + stringBufferName + "删除");
                bufferedWriter.write("\tInteger deleteBy" + stringBufferName + "(" + stringBufferParam + ");");
                bufferedWriter.newLine();
                bufferedWriter.newLine();
            }


            bufferedWriter.write("}");
            bufferedWriter.flush();
        }catch (Exception e){
            logger.info("创建mapper失败", e.getMessage());
            e.printStackTrace();
        }finally {
            if(bufferedWriter != null) {
                bufferedWriter.close();
            }
            if(outputStreamWriter != null) {
                outputStreamWriter.close();
            }
            if(outputStream != null) {
                outputStream.close();
            }
        }



    }
}
